Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

小樹屋微讀書會 —— 實習篇 速記

小樹屋微讀書會 —— 實習篇 速記

[day-12]AJAX - 網路請求

[day-12]AJAX - 網路請求

[重新理解 C++]  TMP(1): compiling time recursion

[重新理解 C++] TMP(1): compiling time recursion






留言討論